import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import Validate from './common/validate'
import { DocumentBuilder } from '@nestjs/swagger'
import { SwaggerModule } from '@nestjs/swagger/dist'
import { TransformInterceptor } from './interceptor/transform.interceptor'
import { NestExpressApplication } from '@nestjs/platform-express'
import { HttpsOptions } from '@nestjs/common/interfaces/external/https-options.interface'
import * as fs from 'fs'
import * as path from 'path'

async function bootstrap() {
  const httpsOptions: HttpsOptions = {
    key: fs.readFileSync(path.join(__dirname, '../secrets/dabinzuf.top.key')),
    cert: fs.readFileSync(path.join(__dirname, '../secrets/dabinzuf.top_bundle.crt')),
  }
  const app = await NestFactory.create<NestExpressApplication>(AppModule, { httpsOptions })
  // const app = await NestFactory.create<NestExpressApplication>(AppModule)
  const options = new DocumentBuilder()
    .setTitle('租房小程序接口文档')
    .setDescription('接口文档')
    .setVersion('1')
    .build()
  const document = SwaggerModule.createDocument(app, options)
  SwaggerModule.setup('doc', app, document)
  app.useGlobalPipes(new Validate())
  app.setGlobalPrefix('api')
  app.useGlobalInterceptors(new TransformInterceptor())
  app.useStaticAssets('uploads', { prefix: '/uploads' })
  app.useStaticAssets('pdfs', { prefix: '/pdfs' })
  await app.listen(3000)
}
bootstrap()
